package com.ruoyi.smartcity.traffic.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.smartcity.traffic.vo.EchartsOptionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.smartcity.traffic.mapper.TrafficFlowMapper;
import com.ruoyi.smartcity.traffic.domain.TrafficFlow;
import com.ruoyi.smartcity.traffic.service.ITrafficFlowService;

/**
 * 交通流量Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-24
 */
@Service
public class TrafficFlowServiceImpl implements ITrafficFlowService {
    @Autowired
    private TrafficFlowMapper trafficFlowMapper;

    /**
     * 查询交通流量
     *
     * @param id 交通流量主键
     * @return 交通流量
     */
    @Override
    public TrafficFlow selectTrafficFlowById(Long id) {
        return trafficFlowMapper.selectTrafficFlowById(id);
    }

    /**
     * 查询交通流量列表
     *
     * @param trafficFlow 交通流量
     * @return 交通流量
     */
    @Override
    public List<TrafficFlow> selectTrafficFlowList(TrafficFlow trafficFlow) {
        return trafficFlowMapper.selectTrafficFlowList(trafficFlow);
    }

    /**
     * 新增交通流量
     *
     * @param trafficFlow 交通流量
     * @return 结果
     */
    @Override
    public int insertTrafficFlow(TrafficFlow trafficFlow) {
        trafficFlow.setCreateTime(DateUtils.getNowDate());
        return trafficFlowMapper.insertTrafficFlow(trafficFlow);
    }

    /**
     * 修改交通流量
     *
     * @param trafficFlow 交通流量
     * @return 结果
     */
    @Override
    public int updateTrafficFlow(TrafficFlow trafficFlow) {
        trafficFlow.setUpdateTime(DateUtils.getNowDate());
        return trafficFlowMapper.updateTrafficFlow(trafficFlow);
    }

    /**
     * 批量删除交通流量
     *
     * @param ids 需要删除的交通流量主键
     * @return 结果
     */
    @Override
    public int deleteTrafficFlowByIds(Long[] ids) {
        return trafficFlowMapper.deleteTrafficFlowByIds(ids);
    }

    /**
     * 删除交通流量信息
     *
     * @param id 交通流量主键
     * @return 结果
     */
    @Override
    public int deleteTrafficFlowById(Long id) {
        return trafficFlowMapper.deleteTrafficFlowById(id);
    }

    @Override
    public List<String> groupByRoadName() {
        return trafficFlowMapper.groupByRoadName();
    }

    @Override
    public EchartsOptionVO analysis(String roadName) {
        EchartsOptionVO vo = new EchartsOptionVO();
        TrafficFlow trafficFlow = new TrafficFlow();
        trafficFlow.setRoadName(roadName);
        List<TrafficFlow> list = trafficFlowMapper.selectTrafficFlowList(trafficFlow);
        if (!list.isEmpty()) {
            // streetName 分组
            Map<String, List<TrafficFlow>> map = list.stream()
                    .filter(item -> item.getStreetName() != null)
                    .collect(Collectors.groupingBy(TrafficFlow::getStreetName));
            JSONArray yData = new JSONArray();
            JSONArray xData = new JSONArray();
            for (Map.Entry<String, List<TrafficFlow>> entry : map.entrySet()) {
                xData.add(entry.getKey());
                yData.add(entry.getValue().stream().mapToLong(TrafficFlow::getTrafficVolume).sum());
            }
            vo.setYData(yData);
            vo.setXData(xData);
        }
        return vo;
    }

    @Override
    public EchartsOptionVO top10() {
        EchartsOptionVO vo = new EchartsOptionVO();
        List<TrafficFlow> list = trafficFlowMapper.top10();
        Map<String, List<TrafficFlow>> map = list.stream()
                .filter(item -> item.getRoadName() != null)
                .collect(Collectors.groupingBy(TrafficFlow::getRoadName));
        JSONArray yData = new JSONArray();
        JSONArray xData = new JSONArray();
        for (Map.Entry<String, List<TrafficFlow>> entry : map.entrySet()) {
            xData.add(entry.getKey());
            yData.add(entry.getValue().stream().mapToLong(TrafficFlow::getTrafficVolume).sum());
        }
        vo.setYData(yData);
        vo.setXData(xData);
        return vo;
    }

    @Override
    public EchartsOptionVO analysisByTime() {
        EchartsOptionVO vo = new EchartsOptionVO();
        List<TrafficFlow> list = trafficFlowMapper.selectTrafficFlowList(new TrafficFlow());
        Map<Date, List<TrafficFlow>> map = list.stream()
                .filter(item -> item.getCollectionTime() != null)
                .collect(Collectors.groupingBy(TrafficFlow::getCollectionTime));
        JSONArray yData = new JSONArray();
        JSONArray xData = new JSONArray();
        for (Map.Entry<Date, List<TrafficFlow>> entry : map.entrySet()) {
            xData.add(entry.getKey());
            yData.add(entry.getValue().stream().mapToLong(TrafficFlow::getTrafficVolume).sum());
        }
        vo.setYData(yData);
        vo.setXData(xData);
        return vo;
    }

    @Override
    public List<Map<String, Object>> analysisAccumulate() {
        List<Map<String, Object>> vo = new ArrayList<>();
        List<TrafficFlow> list = trafficFlowMapper.selectTrafficFlowList(new TrafficFlow());
        Map<String, List<TrafficFlow>> map = list.stream()
                .filter(item -> item.getRoadName() != null)
                .collect(Collectors.groupingBy(TrafficFlow::getRoadName));

        for (Map.Entry<String, List<TrafficFlow>> entry : map.entrySet()) {
            Map<String, Object> data = new HashMap<>();
            data.put("name", entry.getKey());
            data.put("count", entry.getValue().stream().mapToLong(TrafficFlow::getTrafficVolume).sum());
            vo.add(data);
        }
        return vo;
    }

    @Override
    public List<Map<String, Object>> averageSpeed() {
        List<Map<String, Object>> vo = new ArrayList<>();
        List<TrafficFlow> list = trafficFlowMapper.selectTrafficFlowList(new TrafficFlow());
        // 根据名字去重复
        list = list.stream().filter(item -> item.getRoadName() != null)
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                        new TreeSet<>(Comparator.comparing(TrafficFlow::getRoadName))), ArrayList::new));
        for (TrafficFlow trafficFlow : list) {
            Map<String, Object> data = new HashMap<>();
            data.put("name", trafficFlow.getRoadName());
            data.put("count", trafficFlow.getTravelSpeed());
            vo.add(data);
        }
        return vo;
    }

    @Override
    public EchartsOptionVO travelTime() {
        EchartsOptionVO vo = new EchartsOptionVO();
        List<TrafficFlow> list = trafficFlowMapper.selectTrafficFlowList(new TrafficFlow());
        Map<String, List<TrafficFlow>> map = list.stream()
                .filter(item -> item.getRoadName() != null)
                .collect(Collectors.groupingBy(TrafficFlow::getRoadName));
        JSONArray yData = new JSONArray();
        JSONArray xData = new JSONArray();
        for (Map.Entry<String, List<TrafficFlow>> entry : map.entrySet()) {
            xData.add(entry.getKey());
            yData.add(entry.getValue().stream().mapToLong(TrafficFlow::getTotalTime).sum());
        }
        vo.setYData(yData);
        vo.setXData(xData);
        return vo;
    }
}
